#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from email.mime.text import MIMEText
from email.header import Header
from email.utils import parseaddr, formataddr
import base64

import smtplib


def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))


def main(subject_text, content_text):
    msg = MIMEText(content_text, 'plain', 'utf-8')
    msg['From'] = _format_addr('我的126邮箱 <{0}>'.format('twj9018@163.com'))
    msg['To'] = _format_addr('我的QQ邮箱 <{0}>'.format('553768003@qq.com'))
    msg['Subject'] = Header(subject_text, 'utf-8').encode()

    server = smtplib.SMTP_SSL('smtp.163.com', 994)
    # server.set_debuglevel(1)

    _password = base64.decodebytes(b'VGFuZ1dKMTI1Nw==\n')
    _password = _password.decode('utf-8')
    server.login('twj9018@163.com', _password)
    server.sendmail('twj9018@163.com', ['553768003@qq.com'], msg.as_string())
    server.quit()


if __name__ == '__main__':
    main('测试smtplib模块', 'It is a testing email from python.')
